package com.example.a26436.newpractise.practise1;

import android.support.annotation.Px;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.a26436.newpractise.R;
import com.example.a26436.newpractise.webview.ToastUtil;

import java.util.ArrayList;

public class Practise1Activity extends AppCompatActivity {

    private ViewPager vpPractise;
    private Button btnA;
    private Button btnB;
    private ArrayList<MyFragment> mFragments = new ArrayList<>();

    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practise1);
        
        findviews();
        initdata();
        initevent();
    }

    private void findviews() {
        vpPractise = findViewById(R.id.vp_activity_practise1);
        btnA = findViewById(R.id.btn_A_activity_practise1);
        btnB = findViewById(R.id.btn_B_activity_practise1);


    }

    private void initdata() {
        mFragments.add(MyFragment.newInstance("主页"));
        mFragments.add(MyFragment.newInstance("我的"));
        btnA.setText("选中了");

        
    }

    private void initevent() {
        mAdapter = new MyAdapter("HAHA.",getSupportFragmentManager());
        vpPractise.setAdapter(mAdapter);
        vpPractise.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, @Px int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0){
                    btnA.setText("选中了");
                    btnB.setText("按钮B");
                }else  if(position ==1){
                    btnA.setText("按钮A");
                    btnB.setText("选中了");

                }else{}

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        btnA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpPractise.setCurrentItem(0);

            }
        });
        btnB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpPractise.setCurrentItem(1);


            }
        });

        
    }
    class  MyAdapter extends FragmentStatePagerAdapter{
        public MyAdapter(String name,FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return mFragments.get((i));
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
